//
//  AppDelegate.m
//  DEMO
//
//  Created by J on 15/11/3.
//  Copyright © 2015年 JZJ. All rights reserved.
//

#import "AppDelegate.h"
#import "OAuthViewController.h"
#import "AccountTool.h"
#import "SDWebImageManager.h"
@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    
    UIUserNotificationSettings* setting = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge categories:nil];
    [application registerUserNotificationSettings:setting];
    //1.创建窗口
    self.window = [[UIWindow alloc]init];
    self.window.frame = [UIScreen mainScreen].bounds;
    //2.显示窗口
    Account * account = [AccountTool account];
    
    //设置根控制器
    if (account) {//如果字典存在说明之前已经登陆成功过
        [self.window switchRootViewController];
    }else{//沙盒里面根本不存在plist
        
        
        self.window.rootViewController = [OAuthViewController new];

    }
    
 
    [self.window makeKeyAndVisible];//3. 成为主窗口

    
    return YES;
}




- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

/**
 *  app进入后台时调用
 *
 *
 */
- (void)applicationDidEnterBackground:(UIApplication *)application {
    /*
     app状态
    1.死亡状态:没有打开app
    2.前台运行状态
    3.后台暂停状态:停止一切动画. 定时器. 多媒体操作,很难再做其他操作
    4.后台运行状态(在后台保存运行)
     */
    //向操作系统申请后台运行的资格, 能维持多久是不确定的
  UIBackgroundTaskIdentifier task = [application beginBackgroundTaskWithExpirationHandler:^{
        //当申请的后台运行时间已经结束 就会调用这个block
      //结束任务
        [application endBackgroundTask:task];
        
    }];
    
    //在Info.plist设置后台模式:Info.plist Required background modes
    
    //搞一个0kb的MP3文件, 没有声音
    //循环播放 异步
    
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}


//整个程序收到内存警告处理
- (void)applicationDidReceiveMemoryWarning:(UIApplication *)application{
    
  SDWebImageManager* manager = [SDWebImageManager sharedManager];
   
    //1.取消下载
    [manager cancelAll];

    //2.清除内存中的所有图片
    [manager.imageCache clearMemory ];
    
    
}



@end
